#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Filename: get_workspace_info.py
# Author: ChenChangSong
# Date: 2025-09-22

"""
功能描述: 获取工作空间信息
https://open.tapd.cn/document/api-doc/API%E6%96%87%E6%A1%A3/api_reference/workspace/get_workspace_info.html
"""
import sys
import os
import time
import random
from comm.common_code import *

def get_workspace_info(project_id=None,token=None,max_retries=5):
    """
    获取工作空间信息
    """
    time.sleep(0.5)
    url = f"https://api.tapd.cn/workspaces/get_workspace_info?workspace_id={project_id}&access_token={token}"

    payload = {}
    
    # 添加重试机制
    retry_count = 0
    while retry_count < max_retries:
        # 获取工作空间信息
        logger.info(f'获取工作空间信息项目ID:{project_id}↓↓↓=================================================↓↓↓')
        response_code, response = api_request(method='GET', url=url, headers={}, data=payload)
        logger.info(f"响应内容: {response}")
        
        if response_code == 200:
            logger.info(f'↑↑↑=================================================↑↑↑')
            return response_code, response
        elif response_code == 429:
            # API限流，等待后重试
            wait_time = (2 ** retry_count) + random.random()
            logger.warning(f"API限流，等待 {wait_time:.2f} 秒后重试 (尝试 {retry_count + 1}/{max_retries})")
            time.sleep(wait_time)
            retry_count += 1
        else:
            logger.error(f"获取工作空间信息失败，响应状态码: {response_code}，响应内容: {response}")
            logger.info(f'↑↑↑=================================================↑↑↑')
            return response_code, None
    
    # 达到最大重试次数仍然失败
    logger.error(f"获取工作空间信息失败，项目ID:{project_id}，已达最大重试次数")
    logger.info(f'↑↑↑=================================================↑↑↑')
    return None, None

if __name__ == "__main__":
    get_workspace_info(project_id='66606973')